<?php
/**
 * Created by PHPStorm
 * User: xieshentao
 * Date: 2021/3/31
 * Email: <939369942@qq.com>
 */
namespace app\common\dao;

use app\common\model\BaseModel;
use think\facade\Db;

abstract class BaseDao{

    /**
     * Notes:
     * @return BaseModel
     * @Author: xiest
     * @Date: 2021/3/31 14:53
     */
    abstract protected function getModel(): string;


    public function getPK(): ?string
    {
        return ($this->getModel())::tablePk();
    }

    public function getTableName()
    {
        return ($this->getModel())::tableName();
    }

    /**
     * @param $id
     * @param string $field
     * @Author: xiest
     * @Date: 2021/3/31 14:553
     */
    public function get($id,$field='*')
    {
        return $this->getModel()::getInstance()->field($field)->where($this->getPK(),$id)->find();
    }


    public function exists(int $id)
    {
        return $this->fieldExists($this->getPk(), $id);
    }


    public function delete(int $id)
    {
        return ($this->getModel())::getDB()->where($this->getPk(), $id)->delete();
    }

    public function updates(array $ids, array $data)
    {
        return ($this->getModel())::getDB()->whereIn($this->getPk(), $ids)->update($data);
    }


    public function insertAll(array $data)
    {
        return ($this->getModel())::getDB()->insertAll($data);
    }


    public function fieldExists($field, $value, ?int $except = null): bool
    {
        $query = ($this->getModel())::getDB()->where($field, $value);
        if (!is_null($except)) $query->where($this->getPk(), '<>', $except);
        return $query->count() > 0;
    }

    /**
     * @param array $where
     * @param string $field
     * @param array $with
     * User: xiest
     * Date: 2021/4/28
     */
    public function getWhere(array $where, string $field = '*', array $with = [])
    {
        return ($this->getModel())::getInstance()->where($where)->when($with, function ($query) use ($with) {
            $query->with($with);
        })->field($field)->find();
    }

    /**
     * @param $data
     * @return BaseModel|\think\Model
     * User: xiest
     * Date: 2021/4/28
     */
    public function create($data)
    {
        return $this->getModel()::create($data);
    }

    /**
     * @param int $id
     * @param array $data
     * @return int
     * User: xiest
     * Date: 2021/4/28
     */
    public function update(int $id, array $data): int
    {
        return ($this->getModel())::getDB()->where($this->getPk(), $id)->update($data);
    }

    /**
     * @param $page
     * @param $limit
     * @param array $where
     * @param string $field
     * @return array
     * User: xiest
     * Date: 2021/4/28
     */
    public function getLimit($page,$limit,$where=[],$field='*')
    {
        return ($this->getModel())::getInstance()->where($where)->page($page,$limit)->field($field)->select()->toArray();
    }


    public function incrNumberField($id,$field,$value = 1)
    {
        return $this->update($id,[$field=>Db::raw("{$field} ".($value > 0 ? '+':'')." {$value}")]);
    }


    /**
     * @param array $where
     * User: xiest
     * Date: 2021/4/28
     */
    public function getCount($where=[])
    {
        return ($this->getModel())::getInstance()->where($where)->count($this->getPK());
    }

    public function getSelect(array $where,string  $field = '*'){
        return ($this->getModel())::getInstance()->where($where)->field($field)->select();
    }

    public function getSearch()
    {
        return ($this->getModel())::getInstance();
    }


}